﻿package  
{
	import flash.display.*;
	import flash.events.*;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.*;
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.core.render.data.RenderHitData;
	import org.papervision3d.core.render.data.RenderSessionData;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.shadematerials.PhongMaterial;
	
	import org.cove.ape.*;
	
	import org.papervision3d.core.math.Plane3D;
	import org.papervision3d.core.math.Sphere3D;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.core.utils.Mouse3D;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.core.geom.Lines3D;
	
	import com.tris.*;
	import com.tris.apevision.shapes.* ;
	
	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	public class MainTest extends PapervisionBase
	{
		private static var pointLight:PointLight3D;
		private var mouse3D:Mouse3D;
		private var keyArray:Array = new Array();
		
	    private var _cmf:ColorMatrixFilter;
		private var _bf:BlurFilter;
		private var _bmd:BitmapData;
		private var _bm:Bitmap;
		
		private var constRect:AVConstraintRect;
		
		public function MainTest() 
		{
			initLighting();
			
			
			mouse3D = new Mouse3D();
			Mouse3D.enabled = true;
			
			stage.frameRate = 60;
			addEventListener(Event.ENTER_FRAME, run);
			stage.addEventListener(MouseEvent.CLICK, onClick);
			var i:Number;
			for (i = 0; i < 222; i++)
			{
				keyArray.push([i,false]);
			}
			stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeysDown);
			stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp);
			
			var cb:PhongMaterial = new PhongMaterial(pointLight, 0xffffff, 0x447700, 20);
			var lineMaterial:LineMaterial = new LineMaterial(0xffaa00);
						
			var cubeyPhonga:PhongMaterial = new PhongMaterial(pointLight, 0xffffff, 0x447700, 20);
			var materials:MaterialsList = new MaterialsList( {
				top: cubeyPhonga,
				bottom: cubeyPhonga,
				left: cubeyPhonga,
				right: cubeyPhonga,
				front: cubeyPhonga,
				back: cubeyPhonga
			});
			var floor:AVRectoid = new AVRectoid(scene, materials, 0, 0, 2000, 1000, 30, 0, true);
			
			var cube:AVRectoid = new AVRectoid(scene, materials, 0, -700, 500, 100, 100, 0, false, 0.5, 0.3, 0);
			var cubeNut:AVRectoid = new AVRectoid(scene, materials, 0, -790, 10, 10, 10, 0, false, 0.5, 0.3, 0);
			var cubeConst:SpringConstraint = new SpringConstraint(cubeNut, cube, 1);
			
			constRect = new AVConstraintRect(defaultGroup, scene, cubeyPhonga, 0, -900, 200, 200, 200, 0.6, false);
			constRect.update();
			scene.addChild( constRect, "theCube1" );
			
			//cube.py = -700;
			
			//var rect:RectangleParticle = new RectangleParticle(400, 0, 50, 50, 0.5);
			//var recty:RectangleParticle = new RectangleParticle(200, 300, 500, 50, 0, true);
			//defaultGroup.addParticle( rect );
			//defaultGroup.addParticle( recty );
			
			
			var ball1:AVSpheroid = new AVSpheroid(scene, cubeyPhonga, -100, -60, 50, false, 1, 0.3);
			var ball2:AVSpheroid = new AVSpheroid(scene, cubeyPhonga, -100, -80, 30, false, 1, 0.3);
			var ball3:AVSpheroid = new AVSpheroid(scene, cubeyPhonga, -100, -100, 40, false, 1, 0.3);
			
			defaultGroup.addParticle(cube);
			defaultGroup.addParticle(cubeNut);
			defaultGroup.addConstraint(cubeConst);
			
			defaultGroup.addParticle( floor );
			defaultGroup.addParticle( ball1 );
			defaultGroup.addParticle( ball2 );
			defaultGroup.addParticle( ball3 );
			
			addPendulum(cb, lineMaterial, 530, -280, 20, 470, -80, 40, defaultGroup);
			addPendulum(cb, lineMaterial, 730, -280, 20, 670, -80, 40, defaultGroup);
			
			addPendulum(cb, lineMaterial, -730, -480, 20, -600, -80, 40, defaultGroup);
			
		}
		
		private function initLighting():void
		{
			pointLight = new PointLight3D(true);
			pointLight.x = 500;
			pointLight.z = 5;
			pointLight.y = -1000;
			scene.addChild(pointLight);
		}
		
		private function checkKeysDown(event:KeyboardEvent):void
		{
			keyArray[event.keyCode][1] = true;
			trace(event.keyCode);
		}
		private function checkKeysUp(event:KeyboardEvent):void
		{
			keyArray[event.keyCode][1]=false;
		}
		private function isKeyDown(X:Number):Boolean
		{
			return keyArray[X][1];
		}
		
		private function addBall():void
		{
			
			//var phonga:PhongMaterial = new PhongMaterial(pointLight, 0xffffff, 0x00bb00, 20);
			var bitywaa:BitmapFileMaterial = new BitmapFileMaterial("goat5.jpg", true);
			var sphere:AVSpheroid = new AVSpheroid(scene, bitywaa, Math.random()*300, -1000, 50, false, 1, Math.random(), 0.1, 0.1);
			defaultGroup.addParticle( sphere );
			trace("goat!");
		}
		private function addPendulum(mat:MaterialObject3D, lineMat:LineMaterial, pegX:Number, pegY:Number, pegR:Number, plumbX:Number, plumbY:Number, plumbR:Number, group:Group):void
		{
			var peg:AVSpheroid = new AVSpheroid(scene, mat, pegX, pegY, pegR, true);
			var plumb:AVSpheroid = new AVSpheroid(scene, mat, plumbX, plumbY, plumbR);
			var pegPlumb:AVSpringConstraint = new AVSpringConstraint(scene, lineMat, peg, plumb, 5 );
			group.addParticle( peg );
			group.addParticle( plumb);
			group.addConstraint( pegPlumb );
		}
		private function onClick(e:MouseEvent):void
		{
			addBall();
			checkMouse();
		}
		private function checkMouse():void
		{
			//var rhd:RenderHitData = new RenderHitData(viewport.hitTestPoint2D(new Point(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY)));
			//mouse3D.updatePosition(rhd);
			//trace(mouse3D.hitTestObject(scene.getChildByName("cuboid"), 1));
		}
		private function checkKeys():void
		{
			//SPace
			if (isKeyDown(32) == true)
			{
				camera.y -= 5;
			}
		}
		private function run(evt:Event):void
		{
			checkKeys();
			constRect.update();
			//cube.px += 1;
			//cube.angle += 3;
			APEngine.step();
			APEngine.paint();
		}
	}
	
	
}